package com.example.mywechat;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout mTabweixin;
    private LinearLayout mTabfriend;
    private LinearLayout mTabaddress;
    private LinearLayout mTabsetting;

    private ImageButton mImgweixin;
    private ImageButton mImgfriend;
    private ImageButton mImgaddress;
    private ImageButton mImgsetting;

    private final Fragment mTab01=new weixinFragment();
    private final Fragment mTab02=new friendFragment();
    private final Fragment mTab03=new addressFragment();
    private final Fragment mTab04=new settingFragment();

    private FragmentManager fm;

    //调整页面的显示，requestWindowFeature去掉界面上多余的部分，调用四个相关的功能函数：
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initEvent();
        initFragment();
        setSelect(0);
    }

    //initView( )将bottom中设计的四个部分中的图标和文字绑定，让用户不管是点击图标还是点击文字都能跳转到相应页面
    private void initView(){
        mTabweixin=findViewById(R.id.id_tab_news);
        mTabfriend=findViewById(R.id.id_tab_friend);
        mTabaddress=findViewById(R.id.id_tab_address);
        mTabsetting=findViewById(R.id.id_tab_setting);

        mImgweixin=findViewById(R.id.id_tab_news_img);
        mImgfriend=findViewById(R.id.id_tab_friend_img);
        mImgaddress=findViewById(R.id.id_tab_address_img);
        mImgsetting=findViewById(R.id.id_tab_setting_img);
    }

    //initEvent( )控制监听范围，只监听屏幕下方四个部分的点击行为
    private void initEvent(){
        mTabweixin.setOnClickListener(this);
        mTabfriend.setOnClickListener(this);
        mTabaddress.setOnClickListener(this);
        mTabsetting.setOnClickListener(this);
    }

    //initFragment( )将Fragment初始化，将四个Fragment放入id_content
    private void initFragment(){
        fm=getSupportFragmentManager();
        FragmentTransaction transaction=fm.beginTransaction();
        transaction.add(R.id.id_content,mTab01);
        transaction.add(R.id.id_content,mTab02);
        transaction.add(R.id.id_content,mTab03);
        transaction.add(R.id.id_content,mTab04);
        transaction.commit();
    }

    //将消息、好友、通讯录和设置四个模块分别标号0、1、2、3，setSelect(int i)在收到 i 的值为0时，显示mTab01界面，
    //并且将图标改成tab_news_pressed（图标未点击时是灰色，点击后是绿色），同理，当 i 的值为1、2、3时，
    //也会做相应的改变：
    private void setSelect(int i){
        FragmentTransaction transaction =fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(mTab01);
                mImgweixin.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                transaction.show(mTab02);
                mImgfriend.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                transaction.show(mTab03);
                mImgaddress.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                transaction.show(mTab04);
                mImgsetting.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    //hideFragment( )在显示一个tab界面时，将其他几个界面隐藏起来
    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(mTab01);
        transaction.hide(mTab02);
        transaction.hide(mTab03);
        transaction.hide(mTab04);
    }

    //onClick( )对用户的行为传递不同的参数，当用户点击消息部分时，将 0 传递给setSelect( )，其他部分同以此类推：
    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        resetImgs();
        switch(v.getId()){
            case R.id.id_tab_news:
                setSelect(0);
                break;
            case R.id.id_tab_friend:
                setSelect(1);
                break;
            case R.id.id_tab_address:
                setSelect(2);
                break;
            case R.id.id_tab_setting:
                setSelect(3);
                break;
            default:
                break;
        }
    }

    //点击其他图标后，将之前的图标变为灰色
    private void resetImgs(){
        mImgweixin.setImageResource(R.drawable.tab_weixin_normal);
        mImgfriend.setImageResource(R.drawable.tab_find_frd_normal);
        mImgaddress.setImageResource(R.drawable.tab_address_normal);
        mImgsetting.setImageResource(R.drawable.tab_settings_normal);
    }
}
